import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class FinancialTracker {
    private JFrame frame; // 主窗口

    public FinancialTracker() {
        initialize();
    }

    private void initialize() {
        // 获取屏幕尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = 450; // 窗口宽度
        int height = 350; // 窗口高度
        int x = (screenSize.width - width) / 2; // 屏幕中心x坐标
        int y = (screenSize.height - height) / 2; // 屏幕中心y坐标

        frame = new JFrame("财务管理");
        frame.setBounds(x, y, width, height); // 设置窗口位置和大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作
        frame.getContentPane().setLayout(new BorderLayout()); // 使用边框布局

        // 创建面板用于放置按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(6, 1, 10, 10)); // 6行1列，水平间距10，垂直间距10
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 添加空白边框

        // 创建按钮并设置监听器
        JButton btnRecordIncome = createButton("1. 记录收入", this::recordIncome);
        JButton btnRecordExpense = createButton("2. 记录支出", this::recordExpense);
        JButton btnManageBudget = createButton("3. 预算管理", this::manageBudget);
        JButton btnManageBills = createButton("4. 账单管理", this::manageBills);
        JButton btnMonthlyStatistics = createButton("5. 月度统计", this::monthlyStatistics);
        JButton btnExitSystem = createButton("6. 退出系统", this::exitSystem);

        // 添加按钮到面板
        buttonPanel.add(btnRecordIncome);
        buttonPanel.add(btnRecordExpense);
        buttonPanel.add(btnManageBudget);
        buttonPanel.add(btnManageBills);
        buttonPanel.add(btnMonthlyStatistics);
        buttonPanel.add(btnExitSystem);

        // 将面板添加到主窗口
        frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);

        // 创建一个透明的面板来保持JFrame的焦点
        JPanel focusPanel = new JPanel();
        focusPanel.setOpaque(false);
        frame.getContentPane().add(focusPanel, BorderLayout.PAGE_END);

        // 添加键盘监听器
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                switch (keyCode) {
                    case KeyEvent.VK_1:
                        recordIncome(null);
                        break;
                    case KeyEvent.VK_2:
                        recordExpense(null);
                        break;
                    case KeyEvent.VK_3:
                        manageBudget(null);
                        break;
                    case KeyEvent.VK_4:
                        manageBills(null);
                        break;
                    case KeyEvent.VK_5:
                        monthlyStatistics(null);
                        break;
                    case KeyEvent.VK_6:
                        exitSystem(null);
                        break;
                    default:
                        // 忽略其他按键
                }
            }
        });

        // 使JFrame能够接收键盘事件
        frame.setFocusable(true);
        frame.requestFocusInWindow();

        // 使透明面板获得焦点
        focusPanel.setFocusable(true);
        focusPanel.requestFocusInWindow();
    }

    private JButton createButton(String text, ActionListener actionListener) {
        JButton button = new JButton(text);
        button.addActionListener(actionListener);
        button.setPreferredSize(new Dimension(120, 30)); // 设置按钮的首选大小
        return button;
    }

    private void recordIncome(ActionEvent e) {
        // 显示记录收入对话框
        new RecordIncomeDialog(frame).setVisible(true);
    }

    private void recordExpense(ActionEvent e) {
        // 显示记录支出对话框
        new RecordExpenseDialog(frame).setVisible(true);
    }

    private void manageBudget(ActionEvent e) {
        // 显示预算管理对话框
        new BudgetManagementDialog(frame).setVisible(true);
    }

    private void manageBills(ActionEvent e) {
        // 显示账单管理对话框
        new BillManagementDialog(frame).setVisible(true);
    }

    private void monthlyStatistics(ActionEvent e) {
        // 显示月度统计对话框
        new MonthlyStatisticsDialog(frame).setVisible(true);
    }

    private void exitSystem(ActionEvent e) {
        int confirm = JOptionPane.showConfirmDialog(frame, "确定要退出系统吗？", "退出确认", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            System.exit(0); // 关闭应用程序
        }
    }

    public void showMainWindow() {
        frame.setVisible(true);
    }
}